package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.zwz.bean.Article;
import com.zwz.dao.ArticleDao;
import com.zwz.service.ArticleService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public boolean insert(Article article) {
        int result = articleDao.insert(article);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean deleteById(Integer aid) {
        int result = articleDao.deleteById(aid);

        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean updateById(Article article) {
        int result = articleDao.updateById(article);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Article getById(Integer aid) {
        Article article = articleDao.selectById(aid);
        return article;
    }

    @Override
    public IPage<Article> getPage(int currentPage, int pageSize, Article article) {
        IPage<Article> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(article.getTitle()), Article::getTitle, article.getTitle());
        lqw.like(Strings.isNotEmpty(article.getContents()),Article::getContents, article.getContents());
        lqw.orderByDesc(Article::getAid);
        articleDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        int result = articleDao.countAll();
        return result;
    }

    @Override
    public List<Article> queryList(Article article) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(article.getTitle()),"title",article.getTitle());
        List<Article> articleList = articleDao.selectList(queryWrapper);

        return articleList;
    }
}
